package spring.module.monitor;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
 * 自定义监控端点常用注解
 *  
 * @author Lucky Yang

 */
@Endpoint(id="customEndpoint")
@Component
public class CustomEndpoint {
    /** 定义两个指标 */
    private Map<String, String> metrics = Map.of("status", "up", "detail", "一切正常");

    @ReadOperation
    public Map<String, String> test3(){
        return new HashMap<>(metrics);
    }

    @ReadOperation
    public Map<String, String> test31(@Selector String name){
        return Map.of(name, metrics.get(name));
    }

    /**
     * 动态修改指标
     * 
     * @param name
     * @param value
     */
    @WriteOperation
    public void test4(@Selector String name,@Nullable String value){
        metrics.computeIfPresent(name, (k, v) -> value);
    }    
}
